package com.wcl.fragmenttest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import org.greenrobot.eventbus.EventBus;

import me.yokeyword.fragmentation.ISupportFragment;
import me.yokeyword.fragmentation.SupportActivity;

public class MainActivity extends SupportActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";

    private Button mButton1;
    private Button mButton2;
    private Button mButton3;
    private Button mButton4;
    private int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        FirstFragment fragment = findFragment(FirstFragment.class);
        if (fragment == null) {
            loadRootFragment(R.id.fl_content, FirstFragment.newInstance());
        }
    }

    private void initView() {
        mButton1 = findViewById(R.id.button1);
        mButton2 = findViewById(R.id.button2);
        mButton3 = findViewById(R.id.button3);
        mButton4 = findViewById(R.id.button4);
        mButton1.setOnClickListener(this);
        mButton2.setOnClickListener(this);
        mButton3.setOnClickListener(this);
        mButton4.setOnClickListener(this);
    }

    @Override
    public void onClick(final View v) {
        FragmentManager supportFragmentManager = getSupportFragmentManager();
        switch (v.getId()) {
            case R.id.button1:
                Fragment fragmentByTag = supportFragmentManager.findFragmentByTag(SecondFragment.class.getSimpleName());
                Log.i(TAG, "onClick: fragmentByTag; " + fragmentByTag);
                break;
            case R.id.button2:
                start(ThirdFragment.newInstance(), ISupportFragment.SINGLETASK);
                break;
            case R.id.button3:
//                if (!(getTopFragment() instanceof SecondFragment)) {
//                    FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
//                    fragmentTransaction.replace(R.id.fl_content, SecondFragment.newInstance());
//                    fragmentTransaction.addToBackStack(SecondFragment.class.getSimpleName());
//                    fragmentTransaction.commit();
//                }

                start(SecondFragment.newInstance(), ISupportFragment.SINGLETASK);
                v.post(new Runnable() {
                    @Override
                    public void run() {
                        popTo(FirstFragment.class, false);

                    }
                });
                break;
            case R.id.button4:
//                new Thread(new Runnable() {
//                    @Override
//                    public void run() {
//                        for (int i = 0; i < 10; i++) {
//                            v.post(new Runnable() {
//                                @Override
//                                public void run() {
//                                    mButton3.performClick();
//                                }
//                            });
//                        }
//                    }
//                }).start();
                Log.i(TAG, "onClick: 主页的fragmeng的FragmentManager: " + supportFragmentManager.hashCode());
                EventBus.getDefault().post(new PopEvent("点击次数: " + count++));
                break;
        }
    }
}
